
package com.sec.android.app.myfiles.navigation;

import com.sec.android.app.myfiles.MyFilesTestRunner;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment.ActionModeType;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.module.local.file.LocalFileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.testFeature;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;

import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(MyFilesTestRunner.class)
public class testNavigationManager {
    private NavigationManager mNavigationMgr;

    @Before
    public void setUp() throws Exception {
        MyFilesFacade.create(testFeature.PROCESS_ID);
        mNavigationMgr = NavigationManager.getInstance(testFeature.PROCESS_ID);
        mNavigationMgr.clear();
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testGetInstance() {
        Assert.assertNotNull((mNavigationMgr));
    }

    @Test
    public void testGetProcessId() {
        int ret = mNavigationMgr.getProcessId();
        Assert.assertEquals(testFeature.PROCESS_ID, ret);
    }

    @Test
    public void testClearInstance() {
        NavigationManager.clearInstance(testFeature.PROCESS_ID);
        Assert.assertTrue(mNavigationMgr.isEmpty());

        mNavigationMgr = NavigationManager.getInstance(testFeature.PROCESS_ID);
    }

    @Test
    public void testClear() {
        mNavigationMgr.clear();
        Assert.assertTrue(mNavigationMgr.isEmpty());

        mNavigationMgr = NavigationManager.getInstance(testFeature.PROCESS_ID);
    }

    @Test
    public void testGetDepth() {
        Assert.assertEquals(0, mNavigationMgr.getDepth());
    }

    @Test
    public void testFromPrivateBox() {
        mNavigationMgr.setFromPrivateBox(true);
        Assert.assertTrue(mNavigationMgr.isFromPrivateBox());
    }

    @Test
    public void testFromPrivateStorage() {
        mNavigationMgr.setFromPrivateStorage(true);
        Assert.assertTrue(mNavigationMgr.isFromPrivateStorage());
    }

    @Test
    public void testGoUp() throws Exception {
        mNavigationMgr.enter(NavigationInfo.getInstance(NavigationMode.Normal, new LocalFileRecord(AppConstants.StoragePath.INTERNAL_ROOT + "/test1")));
        mNavigationMgr.enter(NavigationInfo.getInstance(NavigationMode.Normal, new LocalFileRecord(AppConstants.StoragePath.INTERNAL_ROOT + "/test1" + "/test2")));
        mNavigationMgr.enter(NavigationInfo.getInstance(NavigationMode.Normal, new LocalFileRecord(AppConstants.StoragePath.INTERNAL_ROOT + "/test1" + "/test2" + "/test3")));
        mNavigationMgr.enter(NavigationInfo.getInstance(NavigationMode.Normal, new LocalFileRecord(AppConstants.StoragePath.INTERNAL_ROOT + "/test1" + "/test2" + "/test3" + "/test3")));

        if (mNavigationMgr.getCurInfo() != null && mNavigationMgr.getCurInfo().getCurRecord() != null) {
            mNavigationMgr.goUp(null);
            Assert.assertEquals(3, mNavigationMgr.getDepth());
            Assert.assertEquals(AppConstants.StoragePath.INTERNAL_ROOT + "/test1" + "/test2" + "/test3", mNavigationMgr.getCurInfo().getCurRecord().getFullPath());

            mNavigationMgr.enter(NavigationInfo.getInstance(NavigationMode.Normal, new LocalFileRecord(AppConstants.StoragePath.INTERNAL_ROOT + "/test1")));
            mNavigationMgr.goUp(null);
            Assert.assertEquals(5, mNavigationMgr.getDepth());
            Assert.assertEquals(AppConstants.StoragePath.INTERNAL_ROOT, mNavigationMgr.getCurInfo().getCurRecord().getFullPath());
        }
    }

    @Test
    public void testIsPossibleToActionMode() throws Exception {
        for (NavigationMode navigationMode : NavigationMode.values()) {
            for (ActionModeType actionModeType : ActionModeType.values()) {
                boolean expected = IsPossibleToActionModeExpected(navigationMode, actionModeType);
                AbsMyFilesFragment fragment = getFragment(actionModeType);
                mNavigationMgr.enter(NavigationInfo.getInstance(navigationMode, new LocalFileRecord(AppConstants.StoragePath.INTERNAL_ROOT + "/test1")));
                if (mNavigationMgr.getCurInfo() != null) {
                    mNavigationMgr.getCurInfo().setCurFragment(fragment);
                    String msg = navigationMode + "," + actionModeType;

                    Assert.assertEquals(msg, expected, NavigationManager.isPossibleToActionMode());
                    Assert.assertEquals(msg, true, NavigationManager.isPossibleToActionMode(testFeature.PROCESS_ID));
                }
            }
        }
    }

    private boolean IsPossibleToActionModeExpected(NavigationMode navigationMode, ActionModeType actionModeType) {
        boolean bRet = true;

        if (actionModeType != ActionModeType.NORMAL) {
            bRet = false;
        } else {
            NavigationMode list[] = {
                    NavigationMode.Preview_compress_item,
                    NavigationMode.Select_copy_destination,
                    NavigationMode.Select_move_destination,
                    NavigationMode.Select_remove_from_private,
                    NavigationMode.Select_destination_path,
                    NavigationMode.Select_optimize_storage_backup_destination
            };
            for (NavigationMode mode : list) {
                if (navigationMode == mode) {
                    bRet = false;
                    break;
                }
            }
        }
        return bRet;
    }

    private AbsMyFilesFragment getFragment(ActionModeType actionModeType) {
        AbsMyFilesFragment fragment = PowerMockito.mock(AbsMyFilesFragment.class);
        when(fragment.getActionModeType()).thenReturn(actionModeType);
        when(fragment.getListItemCount()).thenReturn(3);

        switch (actionModeType) {
            case SELECT_FILE_FOLDER_OPERATION:
            case EDIT_FILE_FOLDER_OPERATION:
            case SELECT_SHARE_FILE_OPERATION:
                when(fragment.isSelectActionMode()).thenReturn(true);
                break;
            default:
                when(fragment.isSelectActionMode()).thenReturn(false);
                break;
        }
        return fragment;
    }
}
